<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="记录描述" prop="description">
        <el-input
          v-model="queryParams.description"
          placeholder="请输入记录描述"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="涉及金额" prop="amount">
        <el-input
          v-model="queryParams.amount"
          placeholder="请输入涉及金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="应完成时间" prop="dueTime">
        <el-date-picker clearable
                        v-model="queryParams.dueTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择应完成时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="实际完成时间" prop="completeTime">
        <el-date-picker clearable
                        v-model="queryParams.completeTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择实际完成时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="分数影响" prop="creditImpact">
        <el-input
          v-model="queryParams.creditImpact"
          placeholder="分数影响"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          @click="handleExport"
          v-hasPermi="['creditlife:record']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="recordList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="记录描述" align="center" prop="description" />
      <el-table-column label="涉及金额" align="center" prop="amount" />
      <el-table-column label="应完成时间" align="center" prop="dueTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dueTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="实际完成时间" align="center" prop="completeTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.completeTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="完成状态" align="center" prop="status" />
      <el-table-column label="分数影响" align="center" prop="creditImpact" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户守约记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="recordRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户ID" />
        </el-form-item>
        <el-form-item label="记录描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入记录描述" />
        </el-form-item>
        <el-form-item label="涉及金额" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入涉及金额" />
        </el-form-item>
        <el-form-item label="应完成时间" prop="dueTime">
          <el-date-picker clearable
                          v-model="form.dueTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择应完成时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="实际完成时间" prop="completeTime">
          <el-date-picker clearable
                          v-model="form.completeTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择实际完成时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="信用分影响，分正负" prop="creditImpact">
          <el-input v-model="form.creditImpact" placeholder="请输入信用分影响，分正负" />
        </el-form-item>
      </el-form>
      <template slot="footer">
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { listRecord, getRecord, delRecord, addRecord, updateRecord } from "@/api/creditlife/record"

export default {
  name: "Record",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 用户守约记录表格数据
      recordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        recordType: null,
        description: null,
        amount: null,
        dueTime: null,
        completeTime: null,
        status: null,
        creditImpact: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "用户ID不能为空", trigger: "blur" }
        ],
        recordType: [
          { required: true, message: "记录类型ID不能为空", trigger: "change" }
        ],
        description: [
          { required: true, message: "记录描述不能为空", trigger: "blur" }
        ],
        amount: [
          { required: true, message: "涉及金额不能为空", trigger: "blur" }
        ],
        dueTime: [
          { required: true, message: "应完成时间不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "完成状态不能为空", trigger: "change" }
        ],
        creditImpact: [
          { required: true, message: "信用分影响，分正负不能为空", trigger: "blur" }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  activated() {
    this.getList()
  },
  mounted() {
    this.reset()
  },
  methods: {
    /** 查询用户守约记录列表 */
    getList() {
      this.loading = true
      listRecord(this.queryParams).then(response => {
        this.recordList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        recordType: null,
        description: null,
        amount: null,
        dueTime: null,
        completeTime: null,
        status: null,
        creditImpact: null
      };

      // 确保对话框已打开过
      this.open = true;

      this.$nextTick(() => {
        if (this.$refs.recordRef) {
          this.$refs.recordRef.clearValidate(); // 先清除验证
          this.$refs.recordRef.resetFields();  // 再重置字段
        }
        this.open = false; // 立即关闭，用户不会看到闪烁
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      if (this.$refs.queryRef) {
        this.$refs.queryRef.clearValidate();
        this.$refs.queryRef.resetFields();
      }
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加用户守约记录"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getRecord(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改用户守约记录"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["recordRef"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRecord(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addRecord(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除用户守约记录编号为"' + ids + '"的数据项？').then(() => {
        return delRecord(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('creditlife/record/export', {
        ...this.queryParams
      }, `record_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
